/**
 * 验证字符串在指定字符集之内的规则
 */
package indi.zjh.dp.verify;

import java.util.HashSet;

public class CharSetRule implements Rule {
	
	public static final String NAME_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
	public static final String ENG_SET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String ENG_UPCASE_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String ENG_LOWCASE_SET = "abcdefghijklmnopqrstuvwxyz";
	public static final String DEC_NUM_SET = "0123456789";
	public static final String HEX_NUM_SET = "0123456789abcdefABCDEF";

	protected String charset = "";
	
	public CharSetRule(String charset)
	{
		this.charset = charset;
	}
	
	@Override
	public boolean test(String input) {
		// TODO Auto-generated method stub
		char[] ic = input.toCharArray();
		char[] cc = this.charset.toCharArray();
		if(ic.length == 0)
			return false;
		if(ic.length > 0 && cc.length == 0)
			return false;
		HashSet<Character> is = new HashSet<Character>();
		for(int i = 0; i < ic.length; ++i)
		{
			is.add(ic[i]);
		}
		
		HashSet<Character> cs = new HashSet<Character>();
		for(int i = 0; i < cc.length; ++i)
		{
			cs.add(cc[i]);
		}
		
		is.removeAll(cs);
		
		return is.size() == 0;
	}

	@Override
	public String desc() {
		// TODO Auto-generated method stub
		return "数据应该在指定的字符集["+this.charset+"]内";
	}

}
